from hytest  import *
from lib.api import *
import re,json


def suite_setup():
    INFO('返回token')
    response = requests.post("http://******************/api/v1/auth/userToken",
                             json={"userAccount": "******************", "password": "******************"}
                             )
    global auth_token
    auth_token = str(response.json()["data"]["token"])


class API_2006:
    name = 'API_2006'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：通过子系统查询设备实时状态信息 测试     #2.2')
        INFO('前置条件：token正确，appName: AFC%2CPSD%2CBAS 正确，locationId不填')
        urlpara = {         #参数为可选项
            # 'locationId':349,
            'appName': 'AFC,PSD,BAS'
        }
        response = requests.post("http://******************/api/v1/dataManagement/getPointByAppName",
                                 headers={
                                     "auth-token": auth_token
                                 },
                                 params=urlpara
                                 )
        expectedResult='''
{
    "status": "success", 
    "msg": "查询成功", 
    "code": 0, 
    "type": null, 
    "data": [
        {
            "subAppName": "BAS", 
            "deviceList": [
                {
                    "deviceName": "ESCALATOR002", 
                    "pointList": [
                        {
                            "keyId": 98610722, 
                            "pointName": "ZGZ.BAS.DFT.ESCALATOR02.ddicb.Value", 
                            "dataPtDesc": "电扶梯02关闭状态", 
                            "value": "0", 
                            "deviceName": "ESCALATOR002", 
                            "subAppName": "BAS"
                        }
                    ]
                }, 
                {
                    "deviceName": "ESCALATOR003", 
                    "pointList": [
                        {
                            "keyId": 98610725, 
                            "pointName": "ZGZ.BAS.DFT.ESCALATOR03.ddicb.Value", 
                            "dataPtDesc": "电扶梯03关闭状态", 
                            "value": "0", 
                            "deviceName": "ESCALATOR003", 
                            "subAppName": "BAS"
                        }
                    ]
                }, 
                {
                    "deviceName": "ESCALATOR004", 
                    "pointList": [
                        {
                            "keyId": 98610728, 
                            "pointName": "ZGZ.BAS.DFT.ESCALATOR04.ddicb.Value", 
                            "dataPtDesc": "电扶梯04关闭状态", 
                            "value": "0", 
                            "deviceName": "ESCALATOR004", 
                            "subAppName": "BAS"
                        }
                    ]
                }
            ]
        }
    ]
}
        '''
        outExpectedResult='预期结果:'+expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))           #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))



class API_2007:
    name = 'API_2007'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：通过子系统查询设备实时状态信息 测试     #2.2')
        INFO('前置条件：token过期，appName: AFC%2CPSD%2CBAS 正确，locationId不填')
        urlpara = {         #参数为可选项
            # 'locationId':349,
            'appName': 'AFC,PSD,BAS'
        }
        response = requests.post("http://******************/api/v1/dataManagement/getPointByAppName",
                                 headers={
                                     "auth-token": 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlS2V5IjoiIiwidXNlck5hbWUiOiJ5enlhZG1pbiIsImV4cCI6MTYyMTkzNzIwNCwidXNlcklkIjoieXp5YWRtaW4ifQ.xVITR8Cn0zLrJSA3tKMt48bTyk6tFhlRwjIp4e_yFys'
                                 },
                                 params=urlpara
                                 )
        expectedResult='''
{
    "status": "false",
    "msg": "请求失败",
    "code": 1,
    "type": null,
    "data": 0
}
        '''
        outExpectedResult='预期结果:'+expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))           #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))


class API_2008:
    name = 'API_2008'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：通过子系统查询设备实时状态信息 测试     #2.2')
        INFO('前置条件：token为空，appName: AFC%2CPSD%2CBAS 正确，locationId不填')
        urlpara = {         #参数为可选项
            # 'locationId':349,
            'appName': 'AFC,PSD,BAS'
        }
        response = requests.post("http://******************/api/v1/dataManagement/getPointByAppName",
                                 # headers={
                                 #     "auth-token": 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlS2V5IjoiIiwidXNlck5hbWUiOiJ5enlhZG1pbiIsImV4cCI6MTYyMTkzNzIwNCwidXNlcklkIjoieXp5YWRtaW4ifQ.xVITR8Cn0zLrJSA3tKMt48bTyk6tFhlRwjIp4e_yFys'
                                 # },
                                 params=urlpara
                                 )
        expectedResult='''
{
    "status": "false",
    "msg": "请求失败",
    "code": 1,
    "type": null,
    "data": 0
}
        '''
        outExpectedResult='预期结果:'+expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))           #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))




class API_2009:
    name = 'API_2009'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：通过子系统查询设备实时状态信息 测试     #2.2')
        INFO("前置条件：token正确，'appName': 'ECS%2CFYM'错误，虽然存在，但不再目标appName内，locationId不填")
        urlpara = {         #参数为可选项
            # 'locationId':349,
            'appName': 'ECS,FYM'
        }
        response = requests.post("http://******************/api/v1/dataManagement/getPointByAppName",
                                 headers={
                                     "auth-token": auth_token
                                 },
                                 params=urlpara
                                 )
        expectedResult='''
{
    "status": "false",
    "msg": "请求失败",
    "code": 1,
    "type": null,
    "data": 0
}
        '''
        outExpectedResult='预期结果:'+expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))           #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))


class API_2010:
    name = 'API_2010'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：通过子系统查询设备实时状态信息 测试     #2.2')
        INFO('前置条件：token正确，appName 为空，locationId不填')
        # urlpara = {         #参数为可选项
        #     # 'locationId':349,
        #     'appName': 'ECS'
        # }
        response = requests.post("http://******************/api/v1/dataManagement/getPointByAppName",
                                 headers={
                                     "auth-token": auth_token
                                 }
                                 )
        expectedResult='''
{
    "status": "false",
    "msg": "请求失败",
    "code": 1,
    "type": null,
    "data": 0
}
        '''
        outExpectedResult='预期结果:'+expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))           #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))




class API_2011:
    name = 'API_2011'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：通过子系统查询设备实时状态信息 测试     #2.2')
        INFO('前置条件：token正确，appName: AFC%2CPSD%2CBAS 正确，locationId：349 正确')
        urlpara = {         #参数为可选项
            'locationId':349,
            'appName': 'AFC,PSD,BAS'
        }
        response = requests.post("http://******************/api/v1/dataManagement/getPointByAppName",
                                 headers={
                                     "auth-token": auth_token
                                 },
                                 params=urlpara
                                 )
        expectedResult='''
{
    "status": "success", 
    "msg": "查询成功", 
    "code": 0, 
    "type": null, 
    "data": [
        {
            "subAppName": "BAS", 
            "deviceList": [
                {
                    "deviceName": "ESCALATOR002", 
                    "pointList": [
                        {
                            "keyId": 98610722, 
                            "pointName": "ZGZ.BAS.DFT.ESCALATOR02.ddicb.Value", 
                            "dataPtDesc": "电扶梯02关闭状态", 
                            "value": "0", 
                            "deviceName": "ESCALATOR002", 
                            "subAppName": "BAS"
                        }
                    ]
                }, 
                {
                    "deviceName": "ESCALATOR003", 
                    "pointList": [
                        {
                            "keyId": 98610725, 
                            "pointName": "ZGZ.BAS.DFT.ESCALATOR03.ddicb.Value", 
                            "dataPtDesc": "电扶梯03关闭状态", 
                            "value": "0", 
                            "deviceName": "ESCALATOR003", 
                            "subAppName": "BAS"
                        }
                    ]
                }, 
                {
                    "deviceName": "ESCALATOR004", 
                    "pointList": [
                        {
                            "keyId": 98610728, 
                            "pointName": "ZGZ.BAS.DFT.ESCALATOR04.ddicb.Value", 
                            "dataPtDesc": "电扶梯04关闭状态", 
                            "value": "0", 
                            "deviceName": "ESCALATOR004", 
                            "subAppName": "BAS"
                        }
                    ]
                }
            ]
        }
    ]
}
        '''
        outExpectedResult='预期结果:'+expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))           #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))



class API_2012:
    name = 'API_2012'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：通过子系统查询设备实时状态信息 测试     #2.2')
        INFO("前置条件：token正确，appName: AFC%2CPSD%2CBAS 正确，'locationId':347错误，虽然347存在，但不在目标location内")
        urlpara = {         #参数为可选项
            'locationId':347,
            'appName': 'AFC,PSD,BAS'
        }
        response = requests.post("http://******************/api/v1/dataManagement/getPointByAppName",
                                 headers={
                                     "auth-token": auth_token
                                 },
                                 params=urlpara
                                 )
        expectedResult='''
{
    "status": "false",
    "msg": "请求失败",
    "code": 1,
    "type": null,
    "data": 0
}
        '''
        outExpectedResult='预期结果:'+expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))           #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))


class API_2013:
    name = 'API_2013'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：通过子系统查询设备实时状态信息 测试     #2.2')
        INFO('前置条件：token 为空，appName 为空，locationId 为空 正确')
        # urlpara = {         #参数为可选项
        #     'locationId':347,
        #     'appName': 'AFC,PSD,BAS'
        # }
        response = requests.post("http://******************/api/v1/dataManagement/getPointByAppName",
                                 # headers={
                                 #     "auth-token": auth_token
                                 # },
                                 # params=urlpara
                                 )
        expectedResult='''
{
    "status": "false",
    "msg": "请求失败",
    "code": 1,
    "type": null,
    "data": 0
}
        '''
        outExpectedResult='预期结果:'+expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))           #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))